package com.tcm.order.service.task;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import com.base.genid.generator.UidGenerator;
import com.tcm.core.service.ConfigService;
import com.tcm.doctor.domain.dto.DoctorDTO;
import com.tcm.doctor.service.DoctorService;
import com.tcm.invite.domain.dto.InviteRecordDTO;
import com.tcm.invite.service.InviteRecordService;
import com.tcm.order.dao.mapper.OrderInviterBillMapper;
import com.tcm.order.domain.dto.OrderBillTaskDTO;
import com.tcm.order.entity.OrderInviterBill;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;


/**
 * @author biuco 2020年3月9日上午11:24:36
 * @Description:订单服务实现类
 */
@Service
public class OrderDoctorInviterBillTask implements OrderBillTaskFactory {


    @DubboReference(version = "0.0.1-beta", group = "cached-uid", timeout = 6000, check = false, retries = 3)
    private UidGenerator uidGenerator;

    @Autowired
    private OrderInviterBillMapper orderInviterBillMapper;

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, retries = 3, check = false)
    private ConfigService configService;


    @DubboReference(version = "0.0.1-alpha", timeout = 6000, retries = 3, check = false)
    private InviteRecordService inviteRecordService;

    @DubboReference(version = "0.0.1-alpha", timeout = 6000, retries = 3, check = false)
    private DoctorService doctorService;



    /**
     * 医生推广
     * @param order
     */
    @Override
    public void create(OrderBillTaskDTO order){
        DoctorDTO doctorDTO = doctorService.findDoctorByUserId(order.getDoctorId());
        if(doctorDTO != null){
            //查询业务员
            InviteRecordDTO inviteRecord = inviteRecordService.findByInvitedCode(doctorDTO.getMobile(),"doctor");
            if (ObjectUtil.isNotNull(inviteRecord)){
                //添加记录
                OrderInviterBill orderInviterBill = new OrderInviterBill();
                orderInviterBill.setUserPhone(inviteRecord.getCellphone());
                orderInviterBill.setTarget(doctorDTO.getMobile());
                orderInviterBill.setTargetType("doctor");
                orderInviterBill.setOrderId(order.getId());
                orderInviterBill.setOrderSn(order.getOrderSn());
                orderInviterBill.setOrderId(order.getId());
                Map<String, Object> spread = this.spreadMoney(order.getTotalDrugMoney(),doctorDTO.getMobile(),"doctor");
                orderInviterBill.setMoney((BigDecimal) spread.get("spreadAmount"));
                orderInviterBill.setCreateTime(LocalDateTime.now());
                orderInviterBill.setUpdateTime(LocalDateTime.now());
                orderInviterBill.setInviterLevel(2);
                //业务员结算比例
                BigDecimal settlement = new BigDecimal(configService.findValue("second_distribution", "0.05"));
                orderInviterBill.setMoney(NumberUtil.mul(settlement,
                        (NumberUtil.mul(order.getDrugMoney(),order.getQuantity()))));
                orderInviterBillMapper.insert(orderInviterBill);
            }
        }

    }

    /**
     * 推广提成
     */
    private Map spreadMoney(BigDecimal drugsMoney, String doctor, String spreadType){
        //查询推广员
        InviteRecordDTO inviteRecordDTO = inviteRecordService.findByInvitedCode(doctor, spreadType);
        BigDecimal spreadAmount = new BigDecimal("0");
        String spreadIdPhone = "";
        Map result = new HashMap();
        if(ObjectUtil.isEmpty(inviteRecordDTO)){
            result.put("spreadIdPhone", "");
            result.put("spreadAmount", new BigDecimal("0"));
            return result;
        }
        if(spreadType.equals("doctor")){
            //获取推荐医生的推广人
            spreadIdPhone = inviteRecordDTO.getCellphone();
            //奖励推荐医生
            BigDecimal settlement = new BigDecimal(configService.findValue("second_distribution", "0.05"));
            result.put("spreadPhone", spreadIdPhone);
            result.put("spreadAmount", NumberUtil.mul(settlement, drugsMoney));
        }else if(spreadType.equals("salesman")){
            spreadIdPhone = inviteRecordDTO.getCellphone();
            //业务员结算比例
            BigDecimal settlement = new BigDecimal(configService.findValue("settlement_salesman", "0.08"));
            result.put("spreadIdPhone", spreadIdPhone);
            result.put("spreadAmount", NumberUtil.mul(settlement, drugsMoney));
        }
        return result;
    }




}
